package com.myproject.myblogserver.converter;

import java.util.Collections;
import java.util.List;

import com.myproject.myblogserver.dto.blog.BlogUploadDto;
import com.myproject.myblogserver.dto.blog.BlogPreviewDto;
import com.myproject.myblogserver.dto.blog.BlogShowDto;
import com.myproject.myblogserver.entity.Blog;
import com.myproject.myblogserver.entity.BlogTag;
import com.myproject.myblogserver.entity.User;

public class BlogDtoConverter {

    public static BlogUploadDto convertFormBlog(Blog blog, List<BlogTag> tags) {
        var blogUploadDto = new BlogUploadDto();
        blogUploadDto.setId(blog.getId());
        blogUploadDto.setTitle(blog.getTitle());
        blogUploadDto.setSummary(blog.getSummary());
        blogUploadDto.setContent(blog.getContent());
        blogUploadDto.setTags(convertTags(tags));
        return blogUploadDto;
    }

    public static BlogShowDto convertToBlogShowDto(Blog blog, List<BlogTag> tags) {
        var blogShowDto = new BlogShowDto();
        blogShowDto.setId(blog.getId());
        blogShowDto.setAuthorId(blog.getAuthorId());
        blogShowDto.setCreatedAt(blog.getCreatedAt());
        blogShowDto.setTitle(blog.getTitle());
        blogShowDto.setSummary(blog.getSummary());
        blogShowDto.setContent(blog.getContent());
        blogShowDto.setTags(convertTags(tags));
        return blogShowDto;
    }

    public static BlogPreviewDto convertToBlogPreviewDto(Blog blog, List<String> tags, User user) {
        var blogPreviewDto = BlogPreviewDto.builder().build();
        blogPreviewDto.setId(blog.getId());
        blogPreviewDto.setAuthorId(blog.getAuthorId());
        blogPreviewDto.setCreatedAt(blog.getCreatedAt());
        blogPreviewDto.setTitle(blog.getTitle());
        blogPreviewDto.setSummary(blog.getSummary());
        blogPreviewDto.setAuthorName(user.getName());
        blogPreviewDto.setAuthorAvatar(user.getAvatar());
        blogPreviewDto.setTags(tags);
        return blogPreviewDto;
    }

    private static List<String> convertTags(List<BlogTag> tags) {
        return (tags == null ? Collections.<BlogTag>emptyList() : tags)
                .stream()
                .map(BlogTag::getTagName)
                .toList();
    }
}